<?php

defined('SYSPATH') or die('No direct script access.');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Controller_Portal extends Controller_WebInterface {

    protected $_active_menu;
    protected $_logined = 0;
    protected $_user;
    protected $_node_xml;
    protected $_directory;
    protected $_controller;
    protected $_action;
    protected $_iframe;
    protected $_callback;
    protected $_bar_right;
    protected $_bar_left;

    public function before() {
        $this->setTemplate("portal");
        $this->setActive_menu('Home');

        parent::before();
        $this->_iframe = Arr::get($_GET, 'iframe', 0);
        $this->_callback = Arr::get($_GET, 'callback', '');
        View::bind_global('iframe', $this->_iframe);
        View::bind_global('callback', $this->_callback);
        $this->_controller = strtolower($this->request->controller());

        $session = Session::instance();
        $user = $session->get('user');

        if (empty($user)) {
            $facebook = new Library_Facebook_Base();
            $info = $facebook->getUser();
 
            if (!$info['ok']) {
                $this->redirect($info['urllogin']);
                die;
            }else{
                $s_user = new Model_Service_User();
                $en_user = $s_user->get_by_location_id($info['user']['id'])->get_data();
               
                $a_user = array();
                if($en_user->getId() <= 0){
                    $a_user = $info['user'];
                    $a_user['openid'] = $info['user']['id'];
                    $a_user['id'] = 0;
                }else{
                    $a_user['openid'] = $info['user']['id'];
                    $a_user['name'] = $en_user->getFname();
                    $a_user['email'] = $en_user->getEmail();
                    $a_user['id'] = $en_user->getId();
                    $a_user['avatar'] = $info['user']['avatar'];
                }
                $session->set('user', $a_user);
            }
        }
        $user = $session->get('user');
        
        $this->_user = $user;
   

        View::bind_global('_user', $user);
        View::bind_global('_logined', $this->_logined);
    }

    public function check_security($token) {
        if ($token != Security::token()) {
            $this->redirect(URL::base() . 'user/login/logout');
        }
    }

}

?>